package com.bookstore.controller;

import com.bookstore.common.Result;
import com.bookstore.entity.Address;
import com.bookstore.service.AddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/address")
@Api(tags = "收货地址管理接口")
public class AddressController {

    @Autowired
    private AddressService addressService;

    @PostMapping
    @ApiOperation("添加收货地址")
    public Result<Boolean> addAddress(@RequestBody Address address) {
        return Result.success(addressService.addAddress(address));
    }

    @PutMapping
    @ApiOperation("更新收货地址")
    public Result<Boolean> updateAddress(@RequestBody Address address) {
        return Result.success(addressService.updateAddress(address));
    }

    @DeleteMapping("/{addressId}")
    @ApiOperation("删除收货地址")
    public Result<Boolean> deleteAddress(
            @ApiParam("地址ID") @PathVariable Integer addressId) {
        return Result.success(addressService.deleteAddress(addressId));
    }

    @GetMapping("/user/{userId}")
    @ApiOperation("获取用户收货地址列表")
    public Result<List<Address>> getUserAddresses(
            @ApiParam("用户ID") @PathVariable Integer userId) {
        return Result.success(addressService.getUserAddresses(userId));
    }

    @GetMapping("/default/{userId}")
    @ApiOperation("获取用户默认收货地址")
    public Result<Address> getDefaultAddress(
            @ApiParam("用户ID") @PathVariable Integer userId) {
        return Result.success(addressService.getDefaultAddress(userId));
    }

    @PutMapping("/{addressId}/default")
    @ApiOperation("设置默认收货地址")
    public Result<Boolean> setDefaultAddress(
            @ApiParam("地址ID") @PathVariable Integer addressId) {
        return Result.success(addressService.setDefaultAddress(addressId));
    }
} 